/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

package com.andremion.floatingnavigationview;

import ohos.agp.components.AttrSet;

import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Utils
 *
 * @author hw
 * @since 2021/5/24
 */
public class Utils {
    private static final String TAG = "Floating-Navigation-View";
    private static final Logger SLOGGER = Logger.getLogger(TAG);

    private static Logger createLogger() {
        return SLOGGER;
    }

    /**
     * Log i.
     *
     * @param msg log信息
     */
    public static void logI(String msg) {
        createLogger().log(Level.INFO, msg);
    }

    /**
     * Loge.
     *
     * @param msg log信息
     */
    public static void loge(String msg) {
        createLogger().log(Level.SEVERE, msg);
    }

    /**
     * Gets float from attr.
     *
     * @param attrSet      自定义属性attr
     * @param name         自定义属性name
     * @param defaultValue 自定义属性默认值
     * @return 返回自定义属性值 float from attr
     */
    public static float getFloatFromAttr(AttrSet attrSet, String name, float defaultValue) {
        float value = defaultValue;
        if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
            value = attrSet.getAttr(name).get().getFloatValue();
        }
        return value;
    }

    /**
     * Gets color from attr.
     *
     * @param attrSet      自定义属性attr
     * @param name         自定义属性name
     * @param defaultValue 自定义属性默认值
     * @return 返回自定义属性值 color from attr
     */
    public static int getColorFromAttr(AttrSet attrSet, String name, int defaultValue) {
        int value = defaultValue;
        if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
            value = attrSet.getAttr(name).get().getColorValue().getValue();
        }
        return value;
    }

    /**
     * Gets boolean from attr.
     *
     * @param attrSet      自定义属性attr
     * @param name         自定义属性name
     * @param defaultValue 自定义属性默认值
     * @return 返回自定义属性值 boolean from attr
     */
    public static boolean getBooleanFromAttr(AttrSet attrSet, String name, boolean defaultValue) {
        boolean value = defaultValue;
        if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
            value = attrSet.getAttr(name).get().getBoolValue();
        }
        return value;
    }


    /**
     * Gets max.
     *
     * @param radius 对比数
     * @return 最大的数 max
     */
    public static int getMax(int[] radius) {
        if (radius.length == 0) {
            return 0;
        }
        int max = radius[0];
        for (int rm : radius) {
            if (rm > max) {
                max = rm;
            }
        }
        return max;
    }

    /**
     * Red int.
     *
     * @param color 色值
     * @return 将16进制值转化为rgb int
     */
    public static int red(int color) {
        return (color >> 16) & 0xFF;
    }

    /**
     * Green int.
     *
     * @param color 色值
     * @return 将16进制值转化为rgb int
     */
    public static int green(int color) {
        return (color >> 8) & 0xFF;
    }

    /**
     * Blue int.
     *
     * @param color 色值
     * @return 将16进制值转化为rgb int
     */
    public static int blue(int color) {
        return color & 0xFF;
    }

}
